package com.app.twth.ui.b2btrailer.oblpn;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.ItemTrailerOblpnListBinding;
import com.app.twth.interfaces.MyOnClickListener;

import java.util.ArrayList;
import java.util.List;


public class B2BTrailerOBLPNAdapter extends RecyclerView.Adapter<B2BTrailerOBLPNAdapter.MyViewHolder> {
    private int index = 1;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;
    private List<B2BTrailerLpn> data = new ArrayList<>();

    public B2BTrailerOBLPNAdapter(Context mContext, List<B2BTrailerLpn> data, MyOnClickListener myOnClickListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
    }

    public void setData(List<B2BTrailerLpn> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemTrailerOblpnListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemTrailerOblpnListBinding itemBinding;

        public MyViewHolder(ItemTrailerOblpnListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2BTrailerLpn pick = data.get(position);
            Log.e(">>>", "index:" + index);
            if (index == 1) {
                itemBinding.ivDelete.setVisibility(View.GONE);
                itemBinding.tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            } else {
//                if (pick.getDetailId()==null) {
                itemBinding.ivDelete.setVisibility(View.VISIBLE);
                itemBinding.tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
//                } else {
//                    itemBinding.ivDelete.setVisibility(View.GONE);
//                    itemBinding.tvItem.setTextColor(mContext.getResources().getColor(R.color.color_999999));
//                }
            }
            itemBinding.tvItem.setText(pick.getObLpn());
            itemBinding.ivDelete.setOnClickListener(v -> myOnClickListener.OnClick(position));
        }
    }

}
